# stories/management/commands/create_superadmin.py
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model

class Command(BaseCommand):
    help = '创建Django后台超级管理员'

    def add_arguments(self, parser):
        parser.add_argument('--username', type=str, help='超级管理员用户名')
        parser.add_argument('--email', type=str, help='超级管理员邮箱')
        parser.add_argument('--password', type=str, help='超级管理员密码')

    def handle(self, *args, **options):
        User = get_user_model()
        
        # 获取参数或使用默认值
        username = options.get('username') or 'superadmin'
        email = options.get('email') or 'superadmin@example.com'
        password = options.get('password') or 'superadmin123'
        
        # 检查用户是否已存在
        if User.objects.filter(username=username).exists():
            self.stdout.write(
                self.style.WARNING(
                    f'用户 "{username}" 已存在'
                )
            )
            return
            
        # 创建超级管理员
        User.objects.create_superuser(
            username=username,
            email=email,
            password=password
        )
        
        self.stdout.write(
            self.style.SUCCESS(
                f'成功创建超级管理员用户 "{username}"'
            )
        )
        self.stdout.write(
            f'用户名: {username}'
        )
        self.stdout.write(
            f'邮箱: {email}'
        )
        self.stdout.write(
            f'密码: {password}'
        )